<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Applications.AlloMediaPortal.Classes.Model.SiteCategory");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\SiteCategory;

/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The SiteCategoryDAO is the Data Access Object for the class SiteCategory
 *
 * @package DAO
 */
class SiteCategoryDAO extends AbstractDAO
{

    protected static $_singleton = null;

    public function save(
        Object &$object,
        /*bool*/ $performTransaction=true,
        /*bool*/ $checkSecurity=true
    )
    {

        $ok = true ;

        if ($performTransaction) {
        	$this->_database->startTransaction();
        }

        if (is_null($object->getProfile())) {
            $profileDao = DAOFactory::getDAO('InformationProfile');
            $profile = $profileDao->retrieveByPartialName('Default', true);
            if (!is_null($profile)) {
                $object->setProfile($profile, false, $checkSecurity);
            }
        }

        if (is_null($object->getStyle())) {
            $styleDao = DAOFactory::getDAO('Style');
            $style = $styleDao->retrieveByPartialName('Default', true);
            if (!is_null($style)) {
                $object->setStyle($style, false, $checkSecurity);
            }
        }

        if ($ok) {
            $entries = $object->getEntries();
            if (!is_null($entries)) {
                $entryDao = DAOFactory::getDAO('AlloMediaEntry');
                foreach ($entries as $entry) {
                    $ok = $entryDao->save($entry, false, $checkSecurity);
                }
            }

            if ($ok) {
            	$ok = parent::save($object, false, $checkSecurity);
            }
        }

        if ($performTransaction) {
            if ($ok) {
                $this->_database->commit();
            } else {
                $this->_database->rollback();
            }
        }

    }

    public function delete(
        Object &$object,
        /*bool*/ $performTransaction=true,
        /*bool*/ $checkSecurity=true
    )
    {

        $categoryLinkDao = DAOFactory::getDAO('CategoryLink');

        if ($performTransaction) {
        	$this->_database->startTransaction();
        }

        $ok = true ;
        $sites = $object->getSites();
        foreach ($sites as $categoryLink) {
            $ok = $categoryLinkDao->delete(
                $categoryLink,
                false,
                $checkSecurity
            );
            if (!$ok) {
            	break;
            }
        }

        if ($ok) {
        	$ok = parent::delete($object, false, $checkSecurity);
        }

        if ($performTransaction) {
            if ($ok) {
                $this->_database->commit();
            } else {
                $this->_database->rollback();
            }
        }

        return $ok ;
    }

    public function retrieveByPartialName(
        /*string*/ $partialName,
        /*bool*/ $exactName=false,
        /*bool*/ $checkSecurity=true
    )
    {

        if ($exactName && isNullOrEmptyString($partialName)) {
        	return null ;
        }

        $colNom = new SqlColumn("category_name");

        $value = mb_strtoupper($partialName, 'utf-8');
        if (!$exactName) {
        	$value = '%' . $value . "%" ;
        }

        $sqlValue = new SqlValue($value);

        $upper = new SqlFunction("UPPER");
        $upper->addParameter($colNom);

        $op = null ;
        if ($exactName) {
        	$op = new SqlOperation(SqlOperation::$equal);
        } else {
        	$op = new SqlOperation(SqlOperation::$like);
        }

        if (!is_null($op)) {
            $op->addParameter($upper);
            $op->addParameter($sqlValue);

            $result = $this->retrieveAll(new QueryFilter($op), null, $checkSecurity);

            if (!$exactName) {
            	return $result ;
            } else if (count($result)>0) {
            	return $result[0] ;
            } else {
            	return null ;
            }
        } else {
            return null ;
        }

    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return SiteCategoryDAO    instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new SiteCategoryDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
